<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 用户活动积分变动管理
 *
 * @icon fa fa-circle-o
 */
class UserActivityPointsLog extends Backend
{

    /**
     * UserActivityPointsLog模型对象
     * @var \app\common\model\UserActivityPointsLog
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\UserActivityPointsLog;
        $this->view->assign("typeList", $this->model->getTypeList());
    }



    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['admin','adminuser','user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit)
                    ->each(function ($item){
                        $item['num'] = fixNum($item['num']);
                        $item['before'] = fixNum($item['before']);
                        $item['after'] = fixNum($item['after']);
                    })
            ;

            foreach ($list as $row) {
                
                $row->getRelation('admin')->visible(['username']);
				$row->getRelation('adminuser')->visible(['username']);
				$row->getRelation('user')->visible(['username']);
            }

            $total_fees = (float)\app\common\model\SystemData::where('id', 1)->value('fee');

            $result = array("total" => $list->total(), "rows" => $list->items(), 'total_fees' => $total_fees);

            return json($result);
        }
        return $this->view->fetch();
    }

}
